<?php
// +----------------------------------------------------------------------
// | ThinkPHP [ WE CAN DO IT JUST THINK ]
// +----------------------------------------------------------------------
// | Copyright (c) 2006-2016 http://thinkphp.cn All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: 流年 <liu21st@gmail.com>
// +----------------------------------------------------------------------

// 应用公共文件

error_reporting(E_ERROR | E_PARSE );

/**
 *  系统日志函数，用于记录日志信息
 * @param string $explain
 * @param string $message
 * @param null $path
 */
function sysLogs($explain='未知' ,$message='' ,$path=null)
{
    $syslogs = model("Syslog");
    $data = array();
    if (empty($path)){
        $data['module'] = request()->module();
        $data['controller'] = request()->controller();
        $data['action'] = request()->action();
    } else {
        $arr = explode('/', $path);
        switch (count($arr)){
            case 1:
                $data['module'] = $arr[0];
                break;
            case 2:
                $data['module'] = $arr[0];
                $data['controller'] = $arr[1];
                break;
            case 3:
                $data['module'] = $arr[0];
                $data['controller'] = $arr[1];
                $data['action'] = $arr[2];
                break;
        }
    }
    $data['explain'] = $explain;
    $data['message'] = $message;
    $syslogs->create($data);
}

/**
 * 序列化
 * @param $value
 * @param string $type
 * @return false|string|null
 */
function serializeByArray($value,$type='json'){
    if (empty($value)){
        return null;
    }
    if ($type == 'json'){
        return json_encode($value,JSON_UNESCAPED_UNICODE);
    }
    if (is_array($value)){
        return implode($type,$value);
    } else {
        return $value;
    }
}

/**
 * 反序列化
 * @param $value
 * @param string $type
 * @return false|mixed|string|null
 */
function unSerializeByJson($value, $type='json'){
    if (empty($value)){
        return null;
    }
    if ($type == 'json'){
        return json_decode($value);
    }
    if (strpos($value, $type)){
        return explode($type,$value);
    } else {
        return $value;
    }
}

/**
 * 日期函數
 * @param $value
 * @param string $format
 * @return false|string
 */
function showDate($value, $format='Y-m-d'){
    return date($format, $value);
}

/**
 * 時間函數
 * @param $value
 * @param string $format
 * @return false|string
 */
function showTime($value, $format='Y-m-d H:i:s'){
    return date($format, $value);
}

/**
 * 獲取用戶id
 * @return mixed|null
 */
function getUserInfo(){
    $login_key = \app\admin\controller\Base::$_login_key;
    return  \app\extra\ApiToken::get($login_key);
}
function getUserID(){
    $user = getUserInfo();
    return empty($user)? null : $user['id'];
}
function getUserName(){
    $user = getUserInfo();
    return $user['nickname']? $user['nickname']:$user['username'];
}